// components/camp-activity/camp-goods.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    campGoodsStatus: {
      type: Boolean,
      value: false
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    campGoodsStatus: false,
    addList: [], //整体数据
    fileList: [],
    goodsImg: "",
    goodsName: "",
    memberPrice: "",
    goodsPrice: ""
  },
  lifetimes: {
    ready() {
    }
  },
  /**
   * 组件的方法列表
   */
  methods: {
    afterRead(event){
      var that = this;
      const { file } = event.detail;
      const userInfo = wx.getStorageSync('userInfo') || null;
      const token = userInfo.token
      wx.uploadFile({
        filePath: file.url,
        name: 'file',
        url: 'http://127.0.0.1:8080/common/upload',
        formData: {user: 'test'},
        
        header: {
          'Authorization': `Bearer ${token}`,
          'content-type': 'application/jspn',
        },
        success(res){
          const {fileList = []} = that.data;
          var resData = JSON.parse(res.data);
          console.log("goodsImg res" ,res)
          console.log("goodsImg resData", resData)
          that.setData({
            goodsImg: resData.fileName,
          })
          fileList.push({ ...file,url: res.data});
          that.setData({fileList});
          console.log("fileList",that.data.fileList)
        },
        fail: (err) =>{
          console.console.error("上传图片错误",err);
        }
      });
    },
    //重置数据
    resetData() {
      this.setData({
        // conditionList: result
        goodsName: "",
        memberPrice: "",
        goodsPrice: "",
        introduce: "",
        goodsImg: "",
        fileList: []
      })
    },
    //添加虚拟数据
    addData(e) {
      const object = {
        goodsName: e.detail.value.goodsName,
        memberPrice: Number(e.detail.value.memberPrice),
        goodsPrice: Number(e.detail.value.goodsPrice),
        goodsImg: this.data.goodsImg
      }
      console.log(object);
      var isEmpty = true;
      Object.keys(object).forEach(function (x) {
        if (object[x] === null || object[x] === "") {
          isEmpty = false;
        }
      });
      if (isEmpty) {
        this.triggerEvent("sendData",object)
        this.resetData();
        this.onClose();
      } else {
        wx.showToast({
          title: "信息未填写完整",
          icon: "error",
          duration: 1500
        })
      }
      // console.log(instance.globalData.campData)
      console.log(this.data.addList);
    },
   
    onClose() {
      this.resetData()
      this.setData({
        campGoodsStatus: false
      })
    },
  }
})